package viper.GUI.copy;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

import viper.main.StateManager;
import viper.main.ViperHeader;

/**
 * @author Kevin Sin
 * 
 * Revision number: 1
 * Date created: 1/17/10
 * Date of Last Back-up on SVN: none
 * 
 * <p>JPanel for when the user is in a call.  Displays who you're talking to
 * and the relays being used.
 */
public class InCallPanel extends javax.swing.JPanel {

    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	/** Creates new form LoggedInJPanel */
    public InCallPanel() {
        initComponents();
    }
    

    private javax.swing.JButton jButton1; //End call button
    private javax.swing.JTextArea jTextArea; //Text area to display callee and relays
    
    //Statistics part
    private JInternalFrame internalFrame;
    private JPanel framePanel;
    private JButton clearButton = new JButton("Clear");
	private JLabel statLabel1 = new JLabel();
	private JLabel statLabel2 = new JLabel();
	private JLabel statLabel3 = new JLabel();
	private JLabel statLabel4 = new JLabel();

    /** This method is called from within the constructor to
     * initialize the form.
     */
    private void initComponents() {

        jButton1 = new javax.swing.JButton();
        jTextArea = new javax.swing.JTextArea();
        jTextArea.setEditable(false);

        jButton1.setText("End Call");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(172, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jButton1)
                    .addComponent(jTextArea))
                .addGap(171, 171, 171))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(94, 94, 94)
                .addComponent(jTextArea)
                .addGap(35, 35, 35)
                .addComponent(jButton1)
                .addContainerGap(134, Short.MAX_VALUE))
        );
    }

    /**
     * @param evt
     * End call button pressed.
     */
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        StateManager.INSTANCE.endCall();
    }                          
    
    /**
     * Updates the display to show the relays in use.
     */
    public void update(){
    	ArrayList<Object[]> relays = StateManager.INSTANCE.getRelays();
    	byte divCode = StateManager.INSTANCE.getDiversityCode();
    	
    	String divCodeStr = "(1,0,1)";
    	
    	if(divCode==ViperHeader.CODE_111)
    		divCodeStr = "(1,1,1)";
    	else if(divCode==ViperHeader.CODE_232)
    		divCodeStr = "(2,3,2)";
    	else if(divCode==ViperHeader.CODE_364)
    		divCodeStr = "(3,6,4)";
    	
    	String relaysString = "";
    	if(!relays.isEmpty())
    	{
    		for(Object[] relay : relays)
    		{
    			relaysString+=relay[1] + "\n";
    		}
    	}
    	jTextArea.setText("In call w/ " + StateManager.INSTANCE.getCalleeArray()[1] +
    			" using code " + divCodeStr + 
    			" and Relays: \n" + relaysString
    			);
    	
    	internalFrame = ViperGUI.INSTANCE.getInternalFrame();
		framePanel = ViperGUI.INSTANCE.getFramePanel();
    	setUpInternalFrame();
    }
    
	public void setUpInternalFrame()
	{
		internalFrame.setTitle("Statistics");
		framePanel.removeAll();
		
		statLabel1.setText(" Total Received Packets: " + "0");
		statLabel2.setText(" Total Sent Packets: " + "0");
		statLabel3.setText(" Total Voice Packets: " + "0");
		statLabel4.setText(" Total Voice Packets: " + "0");
		
		clearButton.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent arg0) {
				StateManager.INSTANCE.getCallReceiver().clearStats();
				updateStatsInFrame();
			}
		});
		
		GridLayout gridlayout = new GridLayout(5, 1);
		
		framePanel.setLayout(gridlayout);
		
		framePanel.add(statLabel1);
		framePanel.add(statLabel2);
		framePanel.add(statLabel3);
		framePanel.add(statLabel4);
		framePanel.add(clearButton);
		//framePanel.add(new JLabel("new stats?"));
		
		internalFrame.setVisible(true);
	}
	
	public void updateStatsInFrame() {
		long[] stats = StateManager.INSTANCE.getCallReceiver().getStatistics();
		
		long stat1 = stats[0];
		long stat2 = stats[1];
		long stat3 = stats[2];
		long stat4 = stats[3];
		
		statLabel1.setText(" Total Received Packets: " + stat1);
		statLabel2.setText(" Total Sent Packets: " + stat2);
		statLabel3.setText(" Total Voice Packets: " + stat3);
		statLabel4.setText(" Total Voice Packets: " + stat4);
		statLabel1.repaint();
		statLabel2.repaint();
		statLabel3.repaint();
		statLabel4.repaint();
	}

}
